const myexpress =require("express")
const systemsetRouter =myexpress.Router();
const shujuku =require("./shujuku");

//商品基本信息
systemsetRouter.get("/systemsettings",(req,res)=>{
    let sql ="select * from systemsettings";
    shujuku.query(sql,[],(err,data) =>{
        if (err){
            console.log(err);
            res.send({code:500,message:"数据库出错，请联系管理员"});
        }else {
            res.send({code:200,data:data})
        }
    })
})

//添加
systemsetRouter.post("/addSystemSetting",(req,res) =>{
    let lineNumber =req.body.lineNumber;
    let codeName =req.body.codeName;
    let encodPrefix =req.body.encodPrefix;
    let example =req.body.example;
    let isAddDate =req.body.isAddDate;
    let isAddTime =req.body.isAddTime;
    let remarks =req.body.remarks;
    let serialNumber =req.body.serialNumber;
    let sql= "insert into systemsettings (lineNumber,codeName,encodPrefix,example,isAddDate,isAddTime,remarks,serialNumber) values(?,?,?,?,?,?,?,?)";
    shujuku.query(sql,[lineNumber,codeName,encodPrefix,example,isAddDate,isAddTime,remarks,serialNumber], (err,data) =>{
        if (err){
            res.send({code:500,message:"数据库异常,联系管理员"});
        }else {
            if (data.affectedRows>0){
                res.send({code:200,message:"添加成功"});
            }else {
                res.send({code:201,message:"添加失败"});
            }
        }

    })
})
//删除
systemsetRouter.get("/deleteSystemSetting",(req,res) =>{
    let lineNumber =req.query.lineNumber;
    let sql ="delete from systemsettings where lineNumber=?";
    shujuku.query(sql,[lineNumber],(err,data) =>{
        if (err){
            res.send({code:500,message:"数据库异常"});
        }else {
            if (data.affectedRows>0){
                res.send({code:200,message:"删除成功"});
            }else {
                res.send({code:201,message:"删除失败"});
            }
        }
    })
})
//修改
systemsetRouter.post("/updateSystemSetting",(req,res) =>{
    let lineNumber =req.body.lineNumber;
    let codeName =req.body.codeName;
    let encodPrefix =req.body.encodPrefix;
    let example =req.body.example;
    let isAddDate =req.body.isAddDate;
    let isAddTime =req.body.isAddTime;
    let remarks =req.body.remarks;
    let serialNumber =req.body.serialNumber;
    let sql ="update systemsettings set lineNumber =?,codeName=?,encodPrefix=?,example=?,isAddDate=?, isAddTime=?," +
        "remarks=?,serialNumber=? where lineNumber=?";
    shujuku.query(sql,[lineNumber,codeName,encodPrefix,example,isAddDate, isAddTime,remarks,serialNumber],(err,data) =>{
        if (err){
            res.send({code:500,message:"数据库异常,联系管理员"});
        }else {
            if (data.affectedRows>0){
                res.send({code:200,message:"添加成功"});
            }else {
                res.send({code:201,message:"添加失败"});
            }
        }
    });

})
module.exports = systemsetRouter;


